package com.xw.bo.ks.service;

import com.xw.bo.ks.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
*
* @author： XW -> ~
* @create： 2023/11/8 18:51
*/
@Slf4j
@Service
public class AsyncServer {

    @Async("async_todo")
    public void asyncTest(Integer counter) throws InterruptedException{
        Thread.sleep(1000);
        System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务：" + counter);
    }


    @Async("async_todo")
    public void whileTest(Integer counter) {
        // 80%的占有率
        int busyTime = 8;
        // 20%的占有率
        int idelTime = 2;
        // 开始时间
        long startTime = 0;

        while (true) {
            // 开始时间
            startTime = System.currentTimeMillis();

            /*
             * 运行时间
             */
            while (System.currentTimeMillis() - startTime < busyTime) {
                ;
            }

            // 休息时间
            try {
                Thread.sleep(idelTime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static void main(String[] args) {


        // 获取当前时间
        Date weekAgo = DateUtils.addDateWithDays(new Date(), -7);
        Date tenDayAgo = DateUtils.addDateWithDays(new Date(), -10);
        Date monthAgo = DateUtils.addDateWithDays(new Date(), -30);

        log.info("weekAgo 时间：{}", DateUtils.formatDateByNumber(weekAgo.getTime(), DateUtils.DATE_FORMAT_yyyyMMddHHmmss_H));
        log.info("tenDayAgo 时间：{}", DateUtils.formatDateByNumber(tenDayAgo.getTime(), DateUtils.DATE_FORMAT_yyyyMMddHHmmss_H));
        log.info("monthAgo 时间：{}", DateUtils.formatDateByNumber(monthAgo.getTime(), DateUtils.DATE_FORMAT_yyyyMMddHHmmss_H));

    }



}
